#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLabel>
#include <QProgressBar>
#include <QTimer>
#include <QPushButton>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //创建定时器
    timer = new QTimer(this);
    //没1秒触发一次timeout信号
    timer->start(100);

    //有则返回，没有则创建
    QStatusBar *statusBar = this->statusBar();
    this->setStatusBar(statusBar);

    //显示一个临时消息
//    statusBar->showMessage("hello Qt~",2000);

    //创建控件和action
    label1 = new QLabel("1 hello Qt " );
    QLabel *label2 = new QLabel("2 应用程序输出 ");
    QLabel *label3 = new QLabel("3 编译输出 ");
    progressBar = new QProgressBar();
    progressBar->setRange(0,100);
    progressBar->setValue(0);

    //为定时器绑定信号槽
    connect(timer,&QTimer::timeout,this,&MainWindow::handleAddProgressBar);

    statusBar->addWidget(label1);
    statusBar->addWidget(label2);
    statusBar->addWidget(label3);
    statusBar->addPermanentWidget(progressBar);

    //添加一个按钮
    QPushButton *button = new QPushButton("重置进度条");
    statusBar->addWidget(button);

    connect(button,&QPushButton::clicked,this,&MainWindow::handleReset);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::handleAddProgressBar()
{
    static int timeoutCount = 0;
    timeoutCount++;
    if(timeoutCount >= 20){
        label1->setText("1 Qt 5.12.4 ");
    }
    //更新进度条
    int value = progressBar->value();
    if(value >= 100){
        timer->stop();
        return ;
    }
    progressBar->setValue(value + 5);
}

void MainWindow::handleReset()
{
    //将进度条值为0
    progressBar->setValue(0);
    //启动定时器
    timer->start(100);
}

